Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
8 / 16
CRAP
73.68% covered (warning)
73.68%
28 / 38
ProductRepository
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
8 / 16
29.04
73.68% covered (warning)
73.68%
28 / 38
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 find
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 findAll
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 findBy
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 findOneBy
0.00% covered (danger)
0.00%
0 / 1
2.06
75.00% covered (warning)
75.00%
3 / 4
 getAvailableAttributeIdsToExport
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 findOneByIdentifier
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 getProductsByGroup
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 getProductCountByGroup
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 countAll
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 hasAttributeInFamily
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 getItemsFromIdentifiers
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getIdentifierProperties
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 searchAfter
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 2
 getFilteredProduct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getFilteredProducts
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
<?php
declare(strict_types=1);
/*
 * This file is part of the Akeneo PIM Enterprise Edition.
 *
 * (c) 2017 Akeneo SAS (http://www.akeneo.com)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Akeneo\Pim\Permission\Bundle\Persistence\ORM\EntityWithValue;
use Akeneo\Pim\Enrichment\Component\Product\Model\GroupInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\ProductInterface;
use Akeneo\Pim\Enrichment\Component\Product\Repository\ProductRepositoryInterface;
use Akeneo\Pim\Permission\Component\Authorization\DenyNotGrantedCategorizedEntity;
use Akeneo\Pim\Permission\Component\Factory\FilteredEntityFactory;
use Akeneo\Tool\Component\StorageUtils\Repository\CursorableRepositoryInterface;
use Akeneo\Tool\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
/**
 * Decorates CE product repository to apply permissions.
 *
 * @author Marie Bochu <marie.bochu@akeneo.com>
 */
class ProductRepository extends EntityRepository implements
    ProductRepositoryInterface,
    IdentifiableObjectRepositoryInterface,
    CursorableRepositoryInterface
{
    /** @var ProductRepositoryInterface */
    private $productRepository;
    /** @var FilteredEntityFactory */
    private $filteredProductFactory;
    /** @var DenyNotGrantedCategorizedEntity */
    private $denyNotGrantedCategorizedEntity;
    /**
     * @param EntityManagerInterface          $em
     * @param ProductRepositoryInterface      $productRepository
     * @param FilteredEntityFactory           $filteredProductFactory
     * @param DenyNotGrantedCategorizedEntity $denyNotGrantedCategorizedEntity
     * @param string                          $entityName
     */
    public function __construct(
        EntityManagerInterface $em,
        ProductRepositoryInterface $productRepository,
        FilteredEntityFactory $filteredProductFactory,
        DenyNotGrantedCategorizedEntity $denyNotGrantedCategorizedEntity,
        string $entityName
    ) {
        parent::__construct($em, $em->getClassMetadata($entityName));
        $this->productRepository = $productRepository;
        $this->filteredProductFactory = $filteredProductFactory;
        $this->denyNotGrantedCategorizedEntity = $denyNotGrantedCategorizedEntity;
    }
    /**
     * {@inheritdoc}
     */
    public function find($id, $lockMode = null, $lockVersion = null)
    {
        $product = $this->productRepository->find($id);
        if (null === $product) {
            return null;
        }
        return $this->getFilteredProduct($product);
    }
    /**
     * {@inheritdoc}
     */
    public function findAll()
    {
        $products = $this->productRepository->findAll();
        return $this->getFilteredProducts($products);
    }
    /**
     * {@inheritdoc}
     */
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $products = $this->productRepository->findBy($criteria, $orderBy, $limit, $offset);
        return $this->getFilteredProducts($products);
    }
    /**
     * {@inheritdoc}
     */
    public function findOneBy(array $criteria, array $orderBy = null)
    {
        $product = $this->productRepository->findOneBy($criteria);
        if (null === $product) {
            return null;
        }
        return $this->getFilteredProduct($product);
    }
    /**
     * {@inheritdoc}
     */
    public function getAvailableAttributeIdsToExport(array $productIds)
    {
        return $this->productRepository->getAvailableAttributeIdsToExport($productIds);
    }
    /**
     * {@inheritdoc}
     */
    public function findOneByIdentifier($identifier)
    {
        $product = $this->productRepository->findOneByIdentifier($identifier);
        if (null === $product) {
            return null;
        }
        return $this->getFilteredProduct($product);
    }
    /**
     * {@inheritdoc}
     */
    public function getProductsByGroup(GroupInterface $group, $maxResults)
    {
        return $this->productRepository->getProductsByGroup($group, $maxResults);
    }
    /**
     * {@inheritdoc}
     */
    public function getProductCountByGroup(GroupInterface $group)
    {
        return $this->productRepository->getProductCountByGroup($group);
    }
    /**
     * {@inheritdoc}
     */
    public function countAll(): int
    {
        return $this->productRepository->countAll();
    }
    /**
     * {@inheritdoc}
     */
    public function hasAttributeInFamily($productId, $attributeCode)
    {
        return $this->productRepository->hasAttributeInFamily($productId, $attributeCode);
    }
    /**
     * {@inheritdoc}
     */
    public function getItemsFromIdentifiers(array $identifiers)
    {
        $products = $this->productRepository->getItemsFromIdentifiers($identifiers);
        return $this->getFilteredProducts($products);
    }
    /**
     * {@inheritdoc}
     */
    public function getIdentifierProperties()
    {
        return $this->productRepository->getIdentifierProperties();
    }
    /**
     * {@inheritdoc}
     */
    public function searchAfter(?ProductInterface $product, int $limit): array
    {
        $products = $this->productRepository->searchAfter($product, $limit);
        return $this->getFilteredProducts($products);
    }
    /**
     * Get a single product filtered with only granted data
     *
     * @param ProductInterface $product
     *
     * @return ProductInterface
     */
    private function getFilteredProduct(ProductInterface $product): ProductInterface
    {
        $this->denyNotGrantedCategorizedEntity->denyIfNotGranted($product);
        return $this->filteredProductFactory->create($product);
    }
    /**
     * Get products filtered with only granted data
     *
     * @param ProductInterface[] $products
     *
     * @return array
     */
    private function getFilteredProducts(array $products): array
    {
        $filteredProducts = [];
        foreach ($products as $product) {
            $this->denyNotGrantedCategorizedEntity->denyIfNotGranted($product);
            $filteredProducts[] = $this->filteredProductFactory->create($product);
        }
        return $filteredProducts;
    }
}